import libtcodpy as libtcod

import console
import splash

def report(root, player, score, energy, song, level, hit, miss, jukebox):
    bak = console.Console(root.width, root.height)
    root.blit(0, 0, root.width, root.height, bak, 0, 0)

    if hit + miss:
        ratio = float(hit) / (hit + miss) + 1e-3
        rating = int(ratio * 100)
    else:
        ratio = 1e-3
        rating = 0

    complete = song.progress
    percent = int(complete * 100)

    if player.done == 'Win':
        gold = int((score / 5.0) * (ratio + 0.2))
        fame = int((score / 5.0) * (ratio + 0.2))
        if ratio >= 0.98:
            gold += 100
            fame += 100

        title = "%s Rocks The House" % player.name
        text = """Congratulations, you completed %s with a rating of %d percent.  You earn %d gold and gain
%d fame.""" % (song.name, rating, gold, fame)
        if energy > 1.25:
            text += "  Epic!"
            jukebox.event('good_end')
        elif energy > 0.75:
            text += "  Rock on!"
            jukebox.event('okay_end')
        else:
            text += "  At least you didn't get chomped."
            title += ". Sorta."
            jukebox.event('bad_end')
    elif player.done == 'Fail':
        gold = int((score / 5.0) * (ratio + 0.2) * complete)
        fame = int((score / 5.0) * (ratio + 0.2) * complete)
        if fame < 100:
            fame = -min(int(score / 10) / ratio, player.fame)
            bonus = 'loose'
        else:
            bonus = 'only gain'

        title = "Work On It"
        text = """Epic fail! %s made it %d percent of the way through %s before booed of stage.  You %s 
%d fame and get paid %d gold for your pain and may God have mercy on your soul.""" % (player.name, percent, song.name, 
                                                                                      bonus, fame, gold)
        jukebox.event('bad_end')
    elif player.done == 'Chomped':
        gold = int((score / 5.0) * (ratio + 0.2) * complete)
        fame = int((score / 5.0) * (ratio + 0.2) * complete)
        if fame < 100:
            fame = -min(int(score / 10) / ratio, player.fame)
            bonus = 'loose'
        else:
            bonus = 'only gain'

        title = "Tough Night"
        text = """Wow! That sucked.  %s made it %d percent of the way through %s before being hacked to
bits by the chompers.  You %s %d fame and get paid %d gold for your pain.
Heal up for tomorrow night.""" % (player.name, percent, song.name, bonus, fame, gold)
        jukebox.event('bad_end')
    else: #player.done == 'Quit':
        gold = 0
        if complete < 75:
            fame = -min(100, player.fame)
            bonus = 'loss'
        else:
            fame = int((score / 5.0) * ratio * complete)
            bonus = 'gain'

        title = "%s Couldn't Hack It" % player.name
        text = """When the going got tough, %s got going... right out the door after only %d percent
of %s.  No gold for you and %d %s of fame.""" % (player.name, percent, song.name, fame, bonus)
        jukebox.event('bad_end')

    george = player.gigs.get((song.name, level), 0)
    if score > george:
        player.gigs[(song.name, level)] = score
        bonus = max(50, int(fame / 3.0))
        text = "%s New personal record! Gain %d bonus fame." % (text, bonus)
        fame += bonus

    player.gold += gold
    player.fame = max(0, player.fame + fame)

    text = text.replace('\n', ' ') + '  Press ENTER to continue.'
    width = root.width * 2 / 3
    height = root.height * 2 / 3
    x, y = (root.width - width) / 2, (root.height - height) / 2

    result = splash.Splash(text, width=width, height=height, border=True, title=title, center=False,
                           key_hit=False, wrap=True, background_alpha=0.75)
    result.render(root, x=x, y=y)

    delay = 1.0
    done = False
    while not done:
        root.flush()
        done = root.wait_for_keypress(True).vk in (libtcod.KEY_ESCAPE, libtcod.KEY_ENTER, libtcod.KEY_KPENTER)
        # Easter Egg: Escape also works.

    bak.blit(0, 0, bak.width, bak.height, root, 0, 0)
